package com.bijiawang.user.controller;




import com.bijiawang.user.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;


import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;


@Controller
public class SearchController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/search")
    public String search(@RequestParam("q") String key, Model model, @RequestParam("sort") String sort,@RequestParam("platform") String platform,
                         @RequestParam(value = "PageName" ,defaultValue = "1")int pagename){
        ArrayList<Goods> List = new ArrayList<Goods>();
        Goods[] list = restTemplate.getForObject("http://PROVIDER-SEARCH/testsearch?q={1}&sort={2}&platform={3}", Goods[].class,key,sort,platform);
        if (list != null) {
            Collections.addAll(List, list);
        }else {
            List = null;
        }

        if (platform.equals("All")) {
            platform = "Taobao,JD,DD,Tmall,SuNing";
        }
        int number;
        if (List != null) {
            number = (int) Math.ceil(List.size() / 40.0);
        } else {
            number = 0;
        }

        model.addAttribute("pagename", pagename);
        model.addAttribute("number", number);
        model.addAttribute("platform", platform);
        model.addAttribute("sort", sort);
        model.addAttribute("List", List);
        model.addAttribute("key", key);
        return "search";

    }

}
